package com.speechb.bean;


import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.Serializable;


public class UserBean implements Serializable {
    private String id = null;
    private String username = null;
    private String nickname = null;
    private boolean vip = false;
    private String avatarUrl = null;
    private String openId = null;


    public void setId(String id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public void setVip(boolean vip) {
        this.vip = vip;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    /*  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();*/
    public static UserBean getUserBean() {
        return (UserBean) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute(ConstantParas.COM_USER_BEAN);
    }
    public static void setUserBean(UserBean userBean) {
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().setAttribute(ConstantParas.COM_USER_BEAN,userBean);
    }
    public static String getId() {
        if (getUserBean() == null) {
            return null;
        }
        return getUserBean().id;
    }
    public static boolean isVIP() {
        if (getUserBean() == null) {
            return false;
        }
        return getUserBean().vip;
    }
    public static String getUsername() {
        if (getUserBean() == null) {
            return null;
        }
        return getUserBean().username;
    }

    public static String getNickname() {
        if (getUserBean() == null) {
            return null;
        }
        return getUserBean().nickname;
    }
    public static String getAvatarUrl() {
        if (getUserBean() == null) {
            return "default_avatar.png";
        }
        return StringUtils.isEmpty(getUserBean().avatarUrl) ? "default_avatar.png" : getUserBean().avatarUrl;
    }

    public static String getOpenId() {
        if (getUserBean() == null) {
            return "";
        }
        return getUserBean().openId;
    }

    public String username() {
        return username;
    }
}
